package com.hyjx.framework.component.excel.template;

import com.hyjx.framework.common.CommonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * excel配置模板控制器
 *
 * @author tianK
 */
@Controller
@RequestMapping(value = "excelTemplate")
public class ExcelTemplateController {
    @Resource(name = "excelTemplateService")
    private ExcelTemplateService excelTemplateService;

    /**
     * 进入模板管理模块
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "/gotoExcelTemplate", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoExcelTemplate(ExcelTemplate et) {

        return "/framework/exceltemplate/list";
    }

    /**
     * 根据条件查询模板
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "/queryAll", produces = "application/json; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String queryAll(ExcelTemplate et) {
        return this.excelTemplateService.query(et);
    }

    /**
     * 打开添加模板页面
     *
     * @return
     */
    @RequestMapping(value = "gotoAdd", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ModelAndView gotoAdd() {
        ModelAndView mv = new ModelAndView("/framework/exceltemplate/add");
        return mv;
    }

    /**
     * 添加模板
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "doAdd", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String doAdd(ExcelTemplate et) {
        try {
            et.setPub_excel_template_id(CommonUtil.getUUID32());
            this.excelTemplateService.insert(et);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 打开模板编辑页面
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoEdit(ExcelTemplate et) {

        ModelAndView mv = new ModelAndView("/framework/exceltemplate/edit");
        et = this.excelTemplateService.queryList(et).get(0);
        mv.addObject("et", et);
        return mv;
    }

    /**
     * 编辑模板
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "doEdit", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String doEdit(ExcelTemplate et) {
        try {
            this.excelTemplateService.update(et);
            return "success";
        } catch (Exception e) {
            return "error";
        }

    }

    /**
     * 删除模板
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "doDelete", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String doDelete(ExcelTemplate et) {
        try {
            this.excelTemplateService.delete(et);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    /**
     * 打开模板查看页面
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "gotoView", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoView(ExcelTemplate et) {

        ModelAndView mv = new ModelAndView("/framework/exceltemplate/view");
        et = this.excelTemplateService.queryList(et).get(0);
        mv.addObject("et", et);
        return mv;
    }

    /**
     * 判断模板英文名字是否存在
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "isExist", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String isEnNameExists(ExcelTemplate et) {
        try {
            if (this.excelTemplateService.isEnNameExists(et))
                return "Y";
            return "N";
        } catch (Exception e) {
            return "error";
        }
    }

    /**
     * 编辑时候判断模板英文名字是否重复
     *
     * @param et
     * @return
     */
    @RequestMapping(value = "isExistEdit", produces = "text/html; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String isEnNameExistsEdit(ExcelTemplate et) {
        try {
            if (this.excelTemplateService.isEnNameExistsEdit(et))
                return "Y";
            else
                return "N";
        } catch (Exception e) {
            return "error";
        }
    }
}
